package com.ruantu.clothing.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruantu.clothing.dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruantu.clothing.entity.Permission;
import com.ruantu.clothing.entity.Role;
import com.ruantu.clothing.entity.RolePer;
import com.ruantu.clothing.entity.User;
import com.ruantu.clothing.service.RolePerService;
import com.ruantu.clothing.service.RoleService;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private JGDao jgDao;
    @Autowired
    private UserRoleDao urDao;

    /**
     * 添加用户获取角色列表
     */
    @Override
    public List<Role> findAllRole(User user) {
        user.setXj(jgDao.getJGCount(user.getJgid()));
        List<Role> role = roleDao.getAllRole(user);
        return role;

    }

    /**
     * 维护角色模块用
     */
    @Autowired
    private PermissionDao pDao;
    @Autowired
    private RolePerDao rpDao;

    @Override
    public Map<String, Object> getAllRole(User user) {
        Map<String, Object> map = new HashMap<String, Object>();

        user.setXj(jgDao.getJGCount(user.getJgid()));
        List<Role> roleList = roleDao.getAllRole(user);
        List<Permission> perList = pDao.getAll();
           /* List<Integer> rolePerList=rpDao.findAllPermissionId(roleList.get(0).getId());*/
           /* map.put("rolePerList", rolePerList);*/
        map.put("roleList", roleList);
        map.put("perList", perList);
        return map;

    }


    /**
     * 修改用戶用到
     */
    @Override
    public Map<String, Object> findAllRoleXiu(User user) {

        Map<String, Object> map = new HashMap<String, Object>();
        user.setXj(jgDao.getJGCount(user.getJgid()));
        List<Role> roleList = roleDao.getAllRole(user);
        List<Integer> urList = urDao.findAllroleId(user.getId());
        map.put("allRole", roleList);
        map.put("roleIds", urList);
        return map;

    }

    /***
     * 根据角色名查该条信息
     */
    @Override
    public Role findOneRole(String rolename) {

        return roleDao.getOneRole(rolename);
    }


    /**
     * 添加角色
     */
    @Override
    public Map<String, Object> tianRole(Role role, User u, String rp) {

        Map<String, Object> map = new HashMap<String, Object>();
        //添加角色
        roleDao.addRole(role);
        //根据用户名查出该角色
        Role ownRole = roleDao.getOneRole(role.getRolename());
        //存角色与权限关系list
        List<RolePer> list = new ArrayList<RolePer>();
        rp = rp.substring(0, rp.length() - 1) + "]";
        JSONArray obj = JSONArray.parseArray(rp);// 解析String 成JSONARRy
        for (int i = 0; i < obj.size(); i++) {
            JSONObject jo = (JSONObject) obj.get(i);
            list.add(new RolePer(ownRole.getId(),
                    Integer.parseInt(jo.get("perid").toString())));
        }
        //添加角色权限
        rpDao.addRolePer(list);
        u.setXj(jgDao.getJGCount(u.getJgid()));
        List<Role> roleList = roleDao.getAllRole(u);
        //List<Integer> rolePerList=rpDao.findAllPermissionId(roleList.get(0).getId());
               /*map.put("rolePerList", rolePerList);*/
        map.put("roleList", roleList);

        return map;


    }

    /**
     * 修改角色
     */
    @Override
    public Map<String, Object> gengRole(Role role, User u, String rp) {

        Map<String, Object> map = new HashMap<String, Object>();
        roleDao.updateRole(role);
        //根据用户名查出该角色
        Role ownRole = roleDao.getOneRole(role.getRolename());
        rpDao.delRolePer(ownRole.getId());
        //存角色与权限关系list
        List<RolePer> list = new ArrayList<RolePer>();
        rp = rp.substring(0, rp.length() - 1) + "]";
        JSONArray obj = JSONArray.parseArray(rp);// 解析String 成JSONARRy
        for (int i = 0; i < obj.size(); i++) {
            JSONObject jo = (JSONObject) obj.get(i);
            list.add(new RolePer(ownRole.getId(),
                    Integer.parseInt(jo.get("perid").toString())));
        }
        //添加角色权限
        rpDao.addRolePer(list);
        u.setXj(jgDao.getJGCount(u.getJgid()));
        List<Role> roleList = roleDao.getAllRole(u);
        map.put("roleList", roleList);
        return map;

    }

    /**
     * 删除角色
     */

    @Override
    public Map<String, Object> shanRole(int id, User u) {

        Map<String, Object> map = new HashMap<String, Object>();
        if (urDao.findCount(id) == 0) {
            roleDao.delRole(id);
            rpDao.delRolePer(id);
            u.setXj(jgDao.getJGCount(u.getJgid()));
            List<Role> roleList = roleDao.getAllRole(u);
            map.put("roleList", roleList);
            map.put("code", 0);
        } else {
            map.put("code", 1);
            map.put("msg", "该角色已引用，不能删除！");
        }
        return map;


    }

    @Override
    public Role findOneRole1(String note) {
        return roleDao.getOneRole1(note);
    }


}
